﻿
//Author: 张亚涛
//createtime:2016-02-28 14:29:16
//Description: 

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;

namespace BLL
{

    /// <summary>
    /// 该类提供了一系列操作 "MemberAccount" 表的方法;
    /// </summary>
    public class MemberAccountService
    {
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回MemberAccount表的所有数据  
        /// </summary>
        /// <returns></returns>        
        public static List<MemberAccount> GetAllMemberAccount()
        {
            return MemberAccountBase.GetAllMemberAccount();
        }
        /// <summary>
        /// 根据MemberAccount表的ID字段返回数据  
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>        
        public static MemberAccount GetMemberAccountById(long id)
        {
            return MemberAccountBase.GetMemberAccountById(id);
        }
        /// <summary>
        /// 获得账户余额
        /// </summary>
        /// <param name="openID">微信ID</param>
        /// <returns></returns>       
        public static decimal GetBlance(string openID)
        {
            return MemberAccountBase.GetBlance(openID);
        }
        /// <summary>
        /// 获得退款金额
        /// </summary>
        /// <param name="orderID">订单号</param>
        /// <returns></returns>       
        public static decimal GetRefund(string orderID)
        {
            return MemberAccountBase.GetRefund(orderID);
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="productType">产品分类</param>
        /// <param name="setType">设置分类</param>
        /// <param name="brand">品牌</param>
        /// <param name="price">价格</param>
        /// <param name="order">排序</param>
        /// <param name="PageSize">每页数量</param>
        /// <param name="CurrentIndex">当前页码</param>
        /// <param name="totalPage">总页码</param>
        /// <returns></returns>
        public static DataTable GetListByPage(string openID, int pageSize, int currentIndex, out int totalPage)
        {
            return CommonService.GetDataByPage(GetAllMemberAccount_SQL(openID), "ID", "Date DESC", pageSize, currentIndex, out totalPage);
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据MemberAccount表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回受影响的行数</returns>
        public static int DeleteMemberAccountById(long id)
        {
            return MemberAccountBase.DeleteMemberAccountById(id);
        }
        /// <summary>
        /// 根据月份删除数据 
        /// </summary>
        /// <param name="month">月份</param>
        /// <returns>返回受影响的行数</returns>
        public static int DeleteByMonth(string month)
        {
            return MemberAccountBase.DeleteByMonth(month);
        }
        #endregion
        #region update data
        /// <summary>
        /// 根据MemberAccount表的ID字段更新数据 
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="MemberAccount">memberAccount</param>
        /// <returns>返回受影响的行数</returns>
        public static int UpdateMemberAccountById(MemberAccount memberAccount)
        {
            return MemberAccountBase.UpdateMemberAccountById(memberAccount);
        }
        /// <summary>
        /// 发放返利
        /// </summary>
        /// <param name="month">月份</param>
        /// <returns>返回受影响的行数</returns>
        public static void Grant(string month)
        {
            DeleteByMonth(month);
            DataTable dt = MemberPayService.GetRebateDetail(month);
            foreach (DataRow dr in dt.Rows)
            {
                MemberAccount m = new MemberAccount();
                m.OpenID = dr["OpenID"].ToString();
                m.OpType = EnumHelper.AccountType.返利.ToString();
                m.Money = decimal.Parse(dr["mCount"].ToString());
                m.Memo = month;
                m.Date = DateTime.Now;
                AddMemberAccount(m);
            }
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向MemberAccount表插入一条数据，插入成功则返回自增列数值，插入不成功则返回-1 
        /// </summary>
        /// <param name="memberAccount">MemberAccount</param>        
        /// <returns></returns>
        public static int AddMemberAccount(MemberAccount memberAccount)
        {
            return MemberAccountBase.AddMemberAccount(memberAccount);
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 向MemberAccount表插入一条数据（SQL语句）
        /// </summary>
        /// <param name="m">MemberAccount</param>       
        /// <returns></returns>        
        public static string AddMemberAccount_SQL(MemberAccount m)
        {
            return MemberAccountBase.AddMemberAccount_SQL(m);
        }
        /// <summary>
        /// 返回MemberAccount表的所有数据（SQL语句） 
        /// </summary>
        /// <param name="openID">微信ID</param>
        /// <returns></returns>        
        public static string GetAllMemberAccount_SQL(string openID)
        {
            return MemberAccountBase.GetAllMemberAccount_SQL(openID);
        }
        #endregion
    }
}